/**
 * 
 */
package com.ekingstar.framework.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonBeanProcessor;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;

import com.opensymphony.xwork2.ActionSupport;
import com.ekingstar.framework.domain.Function;
import com.ekingstar.framework.domain.Role;
import com.ekingstar.framework.service.IFunctionService;
import com.ekingstar.framework.service.IRoleService;
import com.ekingstar.framework.service.ISystemParameterService;

/**
 * 
 *
 */
@Action(name="grantDataRight",namespace="/role")
@Result(name="list",value="list",type=ServletActionRedirectResult.class,params={"namespace","/role"})
public class GrantDataRightAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ISystemParameterService systemParameterService;
	private IRoleService roleService;
	public void setFunctionService(IFunctionService functionService) {
		this.functionService = functionService;
	}
	private IFunctionService functionService;
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	private Role role;
	private String node;
	
	public void setNode(String node) {
		this.node = node;
	}
	public void setSystemParameterService(
			ISystemParameterService systemParameterService) {
		this.systemParameterService = systemParameterService;
	}
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	//获取功能树
	public String getChildren(){
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			PrintWriter out = response.getWriter();
			response.setContentType("application/json; charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			List funs = null;
			if (node == null || node.trim().equals("-1") ||node.trim().equals("")){
				funs =  	functionService.getChildren((String)null);
			}else {
				funs =  	functionService.getChildren(node.trim());
			}
			if (funs != null && !funs.isEmpty()){
				final JsonConfig jc = new JsonConfig();
				jc.registerJsonBeanProcessor(Function.class, new JsonBeanProcessor(){
					public JSONObject processBean(Object bean, JsonConfig jsonConfig) {
						// TODO Auto-generated method stub
						Function value = (Function)bean;
						JSONObject rv = new JSONObject();
						rv.element("text", value.getName());
						rv.element("id", value.getId());
						rv.element("leaf", value.getLeaf());
						return rv;
					}
				});
				JSONArray funcs = JSONArray.fromObject(funs,jc);
				out.print(funcs.toString());
			}
				
			
			
		}catch(Throwable e){
			e.printStackTrace();
		}
		return null;
	}
	//获取功能参数值
	public String getFunctionParams(){
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			PrintWriter out = response.getWriter();
			response.setContentType("application/json; charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			if (node != null){
				List fps = functionService.getFunctionParameter(node, role.getId());
				if (fps != null && !fps.isEmpty()){
				JSONArray funcs = JSONArray.fromObject(fps);
				out.print(funcs.toString());
				}
			}
			
			
		}catch(Throwable e){
			e.printStackTrace();
		}
		return null;
	}
	public String input(){
		if (role != null && role.getId() != null){
			role = roleService.viewRole(role.getId());
		}
		return "input";
	}
	public String execute(){
		//roleService.grantFunctionToRole(role);
		roleService.grantFunctionParamsToRole(role);
		
		return "list";
	}
}
